package com.ruoyi.equipment.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.equipment.domain.TEquipment;
import com.ruoyi.equipment.service.ITEquipmentService;
import com.ruoyi.system.service.ISysDeptService;
import io.swagger.models.auth.In;
import org.apache.commons.lang3.ArrayUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.Iterator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 智能设备表Controller
 *
 * @author ruoyi
 * @date 2021-12-15
 */
@RestController
@RequestMapping("/equipment/equipment")
public class TEquipmentController extends BaseController {
    @Autowired
    private ITEquipmentService tEquipmentService;

    @Autowired
    private ISysDeptService deptService;

    /**
     * 查询智能设备表列表
     */
    @PreAuthorize("@ss.hasPermi('equipment:equipment:list')")
    @GetMapping("/list")
    public TableDataInfo list(TEquipment tEquipment) {
        startPage();
        List<TEquipment> list = tEquipmentService.selectTEquipmentList(tEquipment);
        return getDataTable(list);
    }

    /**
     * 导出智能设备表列表
     */
    @PreAuthorize("@ss.hasPermi('equipment:equipment:export')")
    @Log(title = "智能设备表", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TEquipment tEquipment) {
        List<TEquipment> list = tEquipmentService.selectTEquipmentList(tEquipment);
        ExcelUtil<TEquipment> util = new ExcelUtil<TEquipment>(TEquipment.class);
        util.exportExcel(response, list, "智能设备表数据");
    }

    /**
     * 获取智能设备表详细信息
     */
    @PreAuthorize("@ss.hasPermi('equipment:equipment:query')")
    @GetMapping(value = "/{equipmentId}")
    public AjaxResult getInfo(@PathVariable("equipmentId") String equipmentId) {
        return AjaxResult.success(tEquipmentService.selectTEquipmentByEquipmentId(equipmentId));
    }

    /**
     * 新增部署智能设备表
     */
    @PreAuthorize("@ss.hasPermi('equipment:equipment:add')")
    @Log(title = "智能设备表", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TEquipment tEquipment) {
        String equipmentId = tEquipment.getEquipmentId();

        TEquipment tEquipment1 = tEquipmentService.selectTEquipmentByEquipmentId(equipmentId);

        //检查设备是否连接过
        if (tEquipment1 == null) {
            return AjaxResult.error("请检查设备id:" + tEquipment.getEquipmentId() + "输入是否正确,或检查设备的连接情况");
        }

        if (tEquipment1.getStatus() == 0 && tEquipment1.getEquipmentDeploy() == 0) {
            tEquipment.setEquipmentDeploy(1);
            tEquipment.setCreateBy(getUsername());
            return toAjax(tEquipmentService.insertTEquipment(tEquipment));
        } else {
            return AjaxResult.error("请检查设备id:" + tEquipment.getEquipmentId() + "输入是否正确,或检查设备的连接情况");
        }

    }

    /**
     * 修改智能设备表
     */
    @PreAuthorize("@ss.hasPermi('equipment:equipment:edit')")
    @Log(title = "智能设备表", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TEquipment tEquipment) {
        //检查设备是否在线
        String equipmentId = tEquipment.getEquipmentId();
        TEquipment tEquipment1 = tEquipmentService.selectTEquipmentByEquipmentId(equipmentId);
        if (tEquipment1.getStatus() == 1) {
            return AjaxResult.error("设备:" + tEquipment.getEquipmentId() + "不在线");
        }
        return toAjax(tEquipmentService.updateTEquipment(tEquipment));
    }

    /**
     * 删除智能设备表
     */
    @PreAuthorize("@ss.hasPermi('equipment:equipment:remove')")
    @Log(title = "智能设备表", businessType = BusinessType.OTHER)
    @DeleteMapping("/{equipmentIds}")
    public AjaxResult remove(@PathVariable String[] equipmentIds) {
        return toAjax(tEquipmentService.deleteTEquipmentByEquipmentIds(equipmentIds));
    }

    /**
     * 上线智能设备
     */
    @PreAuthorize("@ss.hasPermi('equipment:equipment:remove')")
    @Log(title = "智能设备表", businessType = BusinessType.OTHER)
    @GetMapping("/upEquipment/{equipmentIds}")
    public AjaxResult upEquipment(@PathVariable String[] equipmentIds) {
        return toAjax(tEquipmentService.upTEquipmentByEquipmentIds(equipmentIds));
    }

}
